package com.hbase.api;

import javafx.scene.control.Tab;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：wudl
 * @date ：Created in 2021-12-04 22:55
 * @description：Hbase 的操作
 * @modified By：
 * @version: 1.0
 */

public class HbaseOperate {
    public static void main(String[] args) throws IOException {

        // 创建表
        HbaseOperate.Connection();
        // 添加数据
//        addData();

        getList();
        //删除数据
//        delData();
    }


    public static Connection getConnection() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.161:2181");
        Connection conn = null;
        if (conn == null) {
            return ConnectionFactory.createConnection(conf);
        }

        return conn;
    }

    public static void Connection() throws IOException {

        Admin admin = getConnection().getAdmin();

        TableName wudlHbase = TableName.valueOf("wudlHbase");
        boolean isTable = admin.tableExists(wudlHbase);
        if (!isTable) {
            TableDescriptorBuilder newBuilder = TableDescriptorBuilder.newBuilder(wudlHbase);
            List<ColumnFamilyDescriptor> familList = new ArrayList<>();
            // 添加列簇
            familList.add(ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes()).build());
            // 在表的构造器中添加表的列簇信息
            newBuilder.setColumnFamilies(familList);
            // 通过表构造器，构建表对象
            TableDescriptor build = newBuilder.build();
            // 执行创建表操作
            admin.createTable(build);
        }
        //释放资源
        admin.close();
        // 关闭连接
        getConnection().close();

    }

    /**
     * 往 表中添加一行数据
     */

    public static void addData() throws IOException {
        Admin admin = getConnection().getAdmin();
        TableName wudlHbase = TableName.valueOf("wudlHbase");
        boolean isTable = admin.tableExists(wudlHbase);
        if (isTable) {
            Table table = getConnection().getTable(TableName.valueOf("wudlHbase"));
            for (int i = 0; i < 10; i++) {
                String number = 10000 + i + "";
                String name = "hdfs" + i;
                String address = "广东省" + i;
                String age = 10 + i + "";
                Put put = new Put(number.getBytes());
                put.addColumn("cf".getBytes(), "name".getBytes(), name.getBytes());
                put.addColumn("cf".getBytes(), "age".getBytes(), age.getBytes());
                put.addColumn("cf".getBytes(), "address".getBytes(), address.getBytes());
                table.put(put);
            }
            table.close();
            getConnection().close();
            System.out.println("数据添加成功-------------->");
        }
    }

    /**
     * 查询数据
     */
    public static void getList() throws IOException {
        Admin admin = getConnection().getAdmin();
        Table table = getConnection().getTable(TableName.valueOf("wudluser"));
        // 获取row 为4949191 的数据
        Get get = new Get("201095ee_1607446800000".getBytes());
        Result result = table.get(get);
        List<Cell> cells = result.listCells();
        for (Cell cell : cells) {
            byte[] rowKeyBytes = CellUtil.cloneRow(cell);
            byte[] familyBytes = CellUtil.cloneFamily(cell);
            byte[] qualifierBytes = CellUtil.cloneQualifier(cell);
            byte[] valueBytes = CellUtil.cloneValue(cell);
            String rowKey = Bytes.toString(rowKeyBytes);
            String family = Bytes.toString(familyBytes);
            String qualifier = Bytes.toString(qualifierBytes);

            if (qualifier.equals("NUM_CURRENT") || qualifier.equals("NUM_PREVIOUS") || qualifier.equals("TOTAL_MONEY") || qualifier.equals("NUM_USAGE")) {
                Integer value = Bytes.toInt(valueBytes);
                System.out.println("rowkey111-->" + rowKey + ":family--->" + family + "--;qualue--->" + qualifier + "---;value----->" + value);
            } else {
                String value = Bytes.toString(valueBytes);
                System.out.println("rowkey222-->" + rowKey + ":family--->" + family + "--;qualue--->" + qualifier + "---;value----->" + value);
            }
        }

        table.close();
        getConnection().close();
        System.out.println("查询数据完成-------------->");
    }

    /**
     * 删除一条数据
     * @throws IOException
     */
    public static void delData() throws IOException {
        Table table = getConnection().getTable(TableName.valueOf("wudlHbase"));
        Delete delete = new Delete("4949191".getBytes());
        delete.addFamily("cf".getBytes());
        delete.addColumn("cf".getBytes(), "name".getBytes());
        table.delete(delete);

    }

    public static  void delTable() throws IOException {
        Admin admin = getConnection().getAdmin();
        if (!admin.tableExists(TableName.valueOf("wudlHbase"))){
            admin.disableTable(TableName.valueOf("wudlHbase"));
        }
        admin.disableTable(TableName.valueOf("wudlHbase"));

    }


}
